﻿using System;
using KKSG;
using UnityEngine;
using XUtliPoolLib;

namespace XMainClient
{
	// Token: 0x02000E11 RID: 3601
	internal class XLevelSpawnTask : XLevelBaseTask
	{
		// Token: 0x0600C20B RID: 49675 RVA: 0x00299D7F File Offset: 0x00297F7F
		public XLevelSpawnTask(XLevelSpawnInfo ls) : base(ls)
		{
			this._IsSummonTask = false;
		}

		// Token: 0x0600C20C RID: 49676 RVA: 0x00299D94 File Offset: 0x00297F94
		public XEntity CreateClientMonster(uint id, float yRotate, Vector3 pos, int _waveid)
		{
			XSingleton<XDebug>.singleton.AddLog("CreateClientMonster --------------------id: " + id + " _waveid:" + _waveid + " pos:" + pos.ToString(), null, null, null, null, null);
			Quaternion rotation = Quaternion.Euler(0f, yRotate, 0f);
			XEntity xentity = XSingleton<XEntityMgr>.singleton.CreateEntity(id, pos, rotation, true, uint.MaxValue);
			if (xentity != null)
			{
				xentity.Wave = _waveid;
				xentity.CreateTime = Time.realtimeSinceStartup;
				XAIEventArgs @event = XEventPool<XAIEventArgs>.GetEvent();
				@event.DepracatedPass = true;
				@event.Firer = XSingleton<XEntityMgr>.singleton.Player;
				@event.EventType = 1;
				@event.EventArg = "SpawnMonster";
				XSingleton<XEventMgr>.singleton.FireEvent(@event, 0.05f);
				return xentity;
			}
			return null;
		}

		// Token: 0x0600C20D RID: 49677 RVA: 0x00299E30 File Offset: 0x00298030
		public XEntity CreateServerAttrMonster(UnitAppearance data, float yRotate, Vector3 pos, int _waveid)
		{
			XSingleton<XDebug>.singleton.AddLog("CreateServerAttrMonster --------------------id: " + data.uID + " _waveid:" + _waveid, null, null, null, null, null);
			XAttributes xattributes = XSingleton<XAttributeMgr>.singleton.InitAttrFromServer(data.uID, data.nickid, data.unitType, data.unitName, data.attributes, data.fightgroup, data.isServerControl, data.skills, data.bindskills, new XOutLookAttr((OutLook)null), data.level, 0U);
			xattributes.Outlook.SetData(data.outlook, xattributes.TypeID);
			XEntity xentity;
			if (xattributes.Type == EntitySpecies.Species_Role)
			{
				xentity = XSingleton<XEntityMgr>.singleton.Add(XSingleton<XEntityMgr>.singleton.CreateRole(xattributes, pos, Quaternion.Euler(0f, yRotate, 0f), false, false));
			}
			else
			{
				xentity = XSingleton<XEntityMgr>.singleton.Add(XSingleton<XEntityMgr>.singleton.CreateEntity(xattributes, pos, Quaternion.Euler(0f, yRotate, 0f), false));
			}
			if (xentity != null)
			{
				xentity.Wave = _waveid;
				if (XSingleton<XScene>.singleton.SceneType == SceneType.SCENE_BOSSRUSH)
				{
					XLevelDocument specificDocument = XDocuments.GetSpecificDocument<XLevelDocument>(XLevelDocument.uuID);
					BossRushTable.RowData bossRushConfig = specificDocument.GetBossRushConfig(XSingleton<XScene>.singleton.SceneID, (uint)(XSingleton<XLevelStatistics>.singleton.ls._total_kill + 1));
					if (data != null)
					{
						float num = 1f;
						double attr = xentity.Attributes.GetAttr(XAttributeDefine.XAttr_MaxHP_Basic);
						double value = attr * (double)num;
						xentity.Attributes.SetAttr(XAttributeDefine.XAttr_MaxHP_Basic, value);
						xentity.Attributes.SetAttr(XAttributeDefine.XAttr_CurrentHP_Basic, value);
					}
				}
				if (data.IsDead)
				{
					XSingleton<XDeath>.singleton.DeathDetect(xentity, null, true);
					return null;
				}
				else
				{
					return xentity;
				}
			}
			return null;
		}

		// Token: 0x0600C20E RID: 49678 RVA: 0x00299FDC File Offset: 0x002981DC
		public override bool Execute(float time)
		{
			base.Execute(time);
			XLevelDynamicInfo xlevelDynamicInfo = null;
			//bool flag = !this._IsSummonTask;
			if (!this._IsSummonTask)
			{
				xlevelDynamicInfo = this._spawner.GetWaveDynamicInfo(this._id);
				//bool flag2 = xlevelDynamicInfo == null;
				if (xlevelDynamicInfo == null)
				{
					return true;
				}
			}
			XEntity xentity = null;
			//bool flag3 = this._SpawnType == LevelSpawnType.Spawn_Source_Monster;
			if (this._SpawnType == LevelSpawnType.Spawn_Source_Monster)
			{
				XSingleton<XDebug>.singleton.AddWarningLog("CreateClientMonster ------ _EnemyID:" + this._EnemyID);
				xentity = this.CreateClientMonster(this._EnemyID, (float)this._MonsterRotate, this._MonsterPos + new Vector3(0f, 0.02f, 0f), this._id);
				XSingleton<XLevelStatistics>.singleton.ls.AddLevelSpawnEntityCount(xentity.ID);
			}
			else
			{
				//bool flag4 = this._SpawnType == LevelSpawnType.Spawn_Source_Doodad;
				if (/*!flag4*/this._SpawnType != LevelSpawnType.Spawn_Source_Doodad)
				{
					UnitAppearance cacheServerMonster = XSingleton<XLevelSpawnMgr>.singleton.GetCacheServerMonster((uint)this._id);
					//bool flag5 = cacheServerMonster != null;
					if (cacheServerMonster != null)
					{
						xentity = this.CreateServerAttrMonster(cacheServerMonster, (float)this._MonsterRotate, this._MonsterPos + new Vector3(0f, 0.02f, 0f), this._id);
						XSingleton<XLevelStatistics>.singleton.ls.AddLevelSpawnEntityCount(xentity.ID);
					}
				}
			}
			//bool flag6 = xlevelDynamicInfo != null;
			if (xlevelDynamicInfo != null)
			{
				//bool flag7 = xentity != null;
				if (xentity != null)
				{
					xlevelDynamicInfo._generateCount++;
					xlevelDynamicInfo._enemyIds.Add(xentity.ID);
				}
				//bool flag8 = xlevelDynamicInfo._generateCount == xlevelDynamicInfo._TotalCount;
				if (xlevelDynamicInfo._generateCount == xlevelDynamicInfo._TotalCount)
				{
					xlevelDynamicInfo._generatetime = time;
				}
				//bool flag9 = xentity != null && xentity.IsBoss;
				if (xentity != null && xentity.IsBoss)
				{
					XSingleton<XTutorialHelper>.singleton.HasBoss = true;
					return false;
				}
			}
			return true;
		}

		// Token: 0x04005289 RID: 21129
		public uint _EnemyID;

		// Token: 0x0400528A RID: 21130
		public int _MonsterRotate;

		// Token: 0x0400528B RID: 21131
		public int _MonsterIndex;

		// Token: 0x0400528C RID: 21132
		public Vector3 _MonsterPos;

		// Token: 0x0400528D RID: 21133
		public LevelSpawnType _SpawnType;

		// Token: 0x0400528E RID: 21134
		public bool _IsSummonTask;
	}
}
